package com.hjqjl.whdemo.feature.feature0.ui.activity.surfaceview;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import androidx.annotation.NonNull;

import com.hjqjl.whdemo.R;
import com.hjqjl.whdemo.base.BaseActivity;
import com.hjqjl.whdemo.databinding.ActivitySurfaceView2Binding;
import com.hjqjl.whdemo.utils.ImageUtil;
import com.hjqjl.whdemo.widget.glowworm.GlowwormManager;

//布朗运动
public class SurfaceView2Activity extends BaseActivity {
    public static void start(Context context) {
        Intent starter = new Intent(context, SurfaceView2Activity.class);
        context.startActivity(starter);
    }

    private GlowwormManager glowwormManager;
    private Bitmap bmpGlowworm;
    private SurfaceView surfaceView;
    private SurfaceHolder surfaceHolder;

    private Canvas canvas = null;
    private Paint paint;
    private int alpha = 0;// Bitmap透明度(0 ~ 255)

    private boolean isRuning = true;

    private ActivitySurfaceView2Binding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivitySurfaceView2Binding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        paint = new Paint();
        paint.setColor(Color.RED);

        surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(@NonNull SurfaceHolder holder) {
                isRuning = true;

                bmpGlowworm = ImageUtil.readBitMap(pActivity, R.drawable.glowworm);
                glowwormManager = new GlowwormManager(bmpGlowworm, 1);

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        while (isRuning) {
                            SystemClock.sleep(100);
                            drawView();
                        }
                    }
                }).start();
            }

            @Override
            public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
                isRuning = false;
                ImageUtil.bitmapRecycle(bmpGlowworm);
            }
        });


    }

    private void drawView() {
        try {
            canvas = surfaceHolder.lockCanvas();
            if (canvas != null) {

                canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG));
                canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
//                backGroundManager.drawBackGround(canvas);//画背景的后景
//
//                babysbreathManager.drawBabysbreath(canvas);//画背景星星，一进去会有一定数量的小星星在天空闪烁
//
//                constellationManager.drawConstellation(canvas);//画星座
//                backGroundManager.drawBackGroundFront(canvas);//画背景的前景
                glowwormManager.drawGlowworm(canvas);//画萤火虫

//                switch (zhuangtai) {//加减分，里面有对星座的控制
//                    case HIGH:
//                        if (gradeflag == true) {
//                            graedManager.drawHighJia(canvas, alpha, highCount);
//                            graedManager.drawGraed(canvas, grade, alpha);
//                        } else {//
//                            MinusAlpha();
//                            graedManager.drawHighJiaOut(canvas, alpha, highCount);
//                            graedManager.drawGraed(canvas, grade, alpha);
//                        }
//                        break;
//                    case MIDDLE:
//                        if (gradeflag == true) {
//                            graedManager.drawMiddleJia(canvas, alpha);
//                            graedManager.drawGraed(canvas, grade, alpha);
//                            if (middleCount == 1) {//== 1执行，为了只执行一次
//                                SoundPlayer.playSound(R.raw.add_score1);
//                            }
//                        } else {
//                            MinusAlpha();
//                            graedManager.drawMiddleJia(canvas, alpha);
//                            graedManager.drawGraed(canvas, grade, alpha);
//                        }
//                        break;
//                    case LOW:
//                        if (gradeflag == true) {
//                            if (total >= 0) {
//                                if (!fristToatl) {// !fristToatl变成true才执行()，要=0一次
//                                    graedManager.drawJian(canvas, alpha, lowCount);
//                                    graedManager.drawGraed(canvas, grade, alpha);
//                                }
//                            }
//                        } else {
//                            if (!fristToatl) {
//                                MinusAlpha();
//                                graedManager.drawJianOut(canvas, alpha, lowCount);
//                                graedManager.drawGraed(canvas, grade, alpha);
//                            }
//                        }
//                        break;
//
//                    default:
//                        break;
//                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (canvas != null) {
                surfaceHolder.unlockCanvasAndPost(canvas);
            }
        }
    }
}